import shutil
import uvicorn
from fastapi import FastAPI, File, UploadFile
from predict_huwai import yuce_huwai
import os
import uuid
from typing import List, Dict

app = FastAPI()

@app.post("/upload_huwai/")
async def upload_file(file: UploadFile = File(...)):
    # 生成一个唯一的文件名来保存上传的图片
    filename = str(uuid.uuid4()) + ".png"
    image_path = os.path.join("./cap_huwai", filename)

    # 确保目录存在
    os.makedirs("./cap_huwai", exist_ok=True)

    # 保存上传的图片到服务器
    with open(image_path, "wb+") as buffer:
        shutil.copyfileobj(file.file, buffer)


    image_info = yuce_huwai(image_path)

    # 构建响应的JSON格式
    response_data = {
        "msg": "操作成功",
        "code": 200,
        "data": {
            "image_info": image_info,
            "image_url": image_path
        }
    }

    return response_data  # FastAPI会自动将其转换为JSON格式


if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)